package Handling;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class makeMoveServlet
 */
@WebServlet(name = "ServletMakeMove", urlPatterns = {"/ServletMakeMove"})

public class ServletMakeMove extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public GameAdmin ga = new GameAdmin();
	
	
	
  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletMakeMove() {
        super();
        // TODO Auto-generated constructor stub        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	HttpSession session = request.getSession(true); 
    	String player =(String) session.getAttribute("PlayerID") ;
    	String game = (String) session.getAttribute("GameID");
    	int playerid = Integer.parseInt(player);
    	int gameid = Integer.parseInt(game);
  
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		try {
			
			String makeMove;
			try {
				makeMove = ga.makeMove(gameid,ga.getFieldID(playerid, gameid),playerid);
				String[] splitResult = makeMove.split("/");
				String nummer = splitResult[0];
				String string = splitResult[1];
				try{
					out.println( "{" + "\"alertnr\":" + "\""+nummer+"\" , " + "\"text\":"+ "\""+string+"\"" + "}" );
			 } finally { out.close(); } 
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("bitchslap");
				e.printStackTrace();
			}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			
		
		
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}